package com.getpebble.android.jskit.webapps;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.jskit.BaseAbstractFragmentActivity;
import com.getpebble.android.jskit.JsKitConstants;
import com.getpebble.android.jskit.NoUiAppsList;
import com.getpebble.android.jskit.NoUiAppsList.IWebappRunHostListOperator;
import com.getpebble.android.jskit.bridge.JsAppContextBridge;
import com.getpebble.android.jskit.webapps.builtins.BlankApp;
import com.getpebble.android.jskit.webapps.builtins.NoUiBlankApp;
import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.UUID;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public abstract class PebbleWebappBaseActivity extends BaseAbstractFragmentActivity implements IWebappRunHostListOperator {
    private static final Long sLaunchCountBase = new Long(0);
    private static long sLaunchCountCurrentAddend = 0;
    private boolean isPaused;
    protected Button mNavButtonNext = null;
    protected Button mNavButtonPrevious = null;
    protected Timer mSceneSwitchDelayTimer = null;

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$jskit$webapps$PebbleWebappBaseActivity$NoUiWebappLifecycleOpsE = new int[NoUiWebappLifecycleOpsE.values().length];
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$jskit$webapps$PebbleWebappBaseActivity$WebappTypeE = new int[WebappTypeE.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$jskit$webapps$PebbleWebappBaseActivity$NoUiWebappLifecycleOpsE[NoUiWebappLifecycleOpsE._INVALID_.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$jskit$webapps$PebbleWebappBaseActivity$NoUiWebappLifecycleOpsE[NoUiWebappLifecycleOpsE.OnCreate.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$jskit$webapps$PebbleWebappBaseActivity$NoUiWebappLifecycleOpsE[NoUiWebappLifecycleOpsE.OnResume.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
            try {
                $SwitchMap$com$getpebble$android$jskit$webapps$PebbleWebappBaseActivity$NoUiWebappLifecycleOpsE[NoUiWebappLifecycleOpsE.OnPause.ordinal()] = 4;
            } catch (NoSuchFieldError e4) {
            }
            try {
                $SwitchMap$com$getpebble$android$jskit$webapps$PebbleWebappBaseActivity$NoUiWebappLifecycleOpsE[NoUiWebappLifecycleOpsE.OnDestroy.ordinal()] = 5;
            } catch (NoSuchFieldError e5) {
            }
            try {
                $SwitchMap$com$getpebble$android$jskit$webapps$PebbleWebappBaseActivity$NoUiWebappLifecycleOpsE[NoUiWebappLifecycleOpsE.ExplicitKill.ordinal()] = 6;
            } catch (NoSuchFieldError e6) {
            }
            try {
                $SwitchMap$com$getpebble$android$jskit$webapps$PebbleWebappBaseActivity$WebappTypeE[WebappTypeE.__INVALID__.ordinal()] = 1;
            } catch (NoSuchFieldError e7) {
            }
            try {
                $SwitchMap$com$getpebble$android$jskit$webapps$PebbleWebappBaseActivity$WebappTypeE[WebappTypeE.Unknown.ordinal()] = 2;
            } catch (NoSuchFieldError e8) {
            }
            try {
                $SwitchMap$com$getpebble$android$jskit$webapps$PebbleWebappBaseActivity$WebappTypeE[WebappTypeE.WebUi.ordinal()] = 3;
            } catch (NoSuchFieldError e9) {
            }
            try {
                $SwitchMap$com$getpebble$android$jskit$webapps$PebbleWebappBaseActivity$WebappTypeE[WebappTypeE.NoUi.ordinal()] = 4;
            } catch (NoSuchFieldError e10) {
            }
        }
    }

    protected static abstract class OperateOnWebappRunHostListData {
        final WebappRunHost WebAppObject;

        abstract void operate(List<WebappRunHost> list, WebappRunHost webappRunHost);

        OperateOnWebappRunHostListData() {
            this.WebAppObject = null;
        }

        OperateOnWebappRunHostListData(WebappRunHost ref) {
            this.WebAppObject = ref;
        }

        void operate(List<WebappRunHost> list) {
            operate(list, this.WebAppObject);
        }
    }

    protected static class AddOnWebappRunHostList extends OperateOnWebappRunHostListData {
        public AddOnWebappRunHostList(WebappRunHost app) {
            super(app);
        }

        void operate(List<WebappRunHost> list, WebappRunHost subject) {
            list.add(subject);
        }
    }

    protected static abstract class QueryOnWebappRunHostListData {
        final WebappRunHost WebAppObject = null;

        abstract Object query(List<WebappRunHost> list, WebappRunHost webappRunHost, Object... objArr);

        QueryOnWebappRunHostListData() {
        }
    }

    protected static class FindInWebappRunHostList extends QueryOnWebappRunHostListData {
        protected FindInWebappRunHostList() {
        }

        Object query(List<WebappRunHost> list, WebappRunHost subject, Object... contextVarArg) {
            if (contextVarArg == null) {
                Trace.debug("PebbleWebappBaseActivity", "FindInWebappRunHostList.query: contextVarArg null");
                return Boolean.FALSE;
            } else if (contextVarArg.length < 1) {
                Trace.debug("PebbleWebappBaseActivity", "FindInWebappRunHostList.query: contextVarArg blank");
                return Boolean.FALSE;
            } else if (list == null) {
                Trace.debug("PebbleWebappBaseActivity", "FindInWebappRunHostList.query: list null");
                return Boolean.FALSE;
            } else {
                String matchUrl = null;
                try {
                    matchUrl = (String) contextVarArg[0];
                } catch (Exception e) {
                    Trace.debug("PebbleWebappBaseActivity", "FindInWebappRunHostList.query", e);
                }
                if (matchUrl == null) {
                    return Boolean.FALSE;
                }
                for (WebappRunHost wappObject : list) {
                    if (wappObject != null && wappObject.lastUrlStringLoaded().equalsIgnoreCase(matchUrl)) {
                        return Boolean.TRUE;
                    }
                }
                return Boolean.FALSE;
            }
        }
    }

    protected enum NoUiWebappLifecycleOpsE {
        _INVALID_,
        OnCreate,
        OnResume,
        OnPause,
        OnDestroy,
        ExplicitKill
    }

    public enum WebappIntentTypeE {
        __INVALID__,
        CloseKill,
        Open
    }

    public enum WebappTypeE {
        __INVALID__,
        Unknown,
        WebUi,
        NoUi
    }

    protected abstract boolean onCloseKillWebappIntent(Intent intent, String str);

    protected abstract boolean onNewWebappCreationIntent(Intent intent, String str);

    public static void LogSimpleDebug(String composed) {
        Trace.debug(PebbleWebappBaseActivity.class.getSimpleName(), composed);
    }

    public static void LogSimpleDebug(String format, Object... args) {
        Trace.debug(PebbleWebappBaseActivity.class.getSimpleName(), String.format(format, args));
    }

    public static void LogSimpleDebug(Activity source, String format, Object... args) {
        Trace.debug(source == null ? "[unknown]" : source.getClass().getSimpleName(), String.format(format, args));
    }

    public static void LogSimpleInfo(String format, Object... args) {
        Trace.info(PebbleWebappBaseActivity.class.getSimpleName(), String.format(format, args));
    }

    public static void LogSimpleWarn(String format, Object... args) {
        Trace.warning(PebbleWebappBaseActivity.class.getSimpleName(), String.format(format, args));
    }

    public static void LogSimpleError(String format, Object... args) {
        Trace.error(PebbleWebappBaseActivity.class.getSimpleName(), String.format(format, args));
    }

    public static long getCurrentLaunchCounter() {
        long longValue;
        synchronized (sLaunchCountBase) {
            longValue = sLaunchCountBase.longValue() + sLaunchCountCurrentAddend;
        }
        return longValue;
    }

    public static long countNewLaunch() {
        long longValue;
        synchronized (sLaunchCountBase) {
            longValue = sLaunchCountBase.longValue();
            long j = sLaunchCountCurrentAddend;
            sLaunchCountCurrentAddend = 1 + j;
            longValue += j;
        }
        return longValue;
    }

    protected void onCreateWithView(Bundle savedInstanceState) {
    }

    protected void onCreate(Bundle savedInstanceState) {
        long thisLaunchNumber = countNewLaunch();
        super.onCreate(savedInstanceState);
        Intent launchingIntent = getIntent();
        try {
            if (launchingIntent.getLongExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_COUNTER", 0) < thisLaunchNumber) {
                LogSimpleDebug(this, String.format("old launch counter: stored = %d , this class is at = %d", new Object[]{Long.valueOf(launchingIntent.getLongExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_COUNTER", 0)), Long.valueOf(thisLaunchNumber)}), new Object[0]);
            }
        } catch (NullPointerException e) {
            Trace.debug("PebbleWebappBaseActivity", "onCreate", e);
        }
        String createWebappParams = null;
        String closeKillWebappParams = null;
        try {
            createWebappParams = launchingIntent.getStringExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_NEW_WEBAPP_PARAMS");
        } catch (NullPointerException e2) {
            Trace.debug("PebbleWebappBaseActivity", "onCreate", e2);
        }
        try {
            closeKillWebappParams = launchingIntent.getStringExtra("PEBBLE_WEBAPP_ACTIVITY_CLOSEKILL_WEBAPP_PARAMS");
        } catch (NullPointerException e22) {
            Trace.debug("PebbleWebappBaseActivity", "onCreate", e22);
        }
        if (closeKillWebappParams != null) {
            onCloseKillWebappIntent(launchingIntent, closeKillWebappParams);
        }
        if (createWebappParams != null) {
            onNewWebappCreationIntent(launchingIntent, createWebappParams);
        }
        JsKit.jsKitAccess().newPebbleWebappBaseActivityInstanceCreated(this);
        LogSimpleDebug("+++++++++++++++++ onCreate");
    }

    protected void onNewIntent(Intent intent) {
        LogSimpleDebug("+++++++++++++++++ onNewIntent");
        String createWebappParams = null;
        String closeKillWebappParams = null;
        try {
            closeKillWebappParams = intent.getStringExtra("PEBBLE_WEBAPP_ACTIVITY_CLOSEKILL_WEBAPP_PARAMS");
            Trace.debug("PebbleWebappBaseActivity", "onNewIntent: closeKillWebappParams = " + closeKillWebappParams);
        } catch (NullPointerException e) {
            Trace.debug("PebbleWebappBaseActivity", "onCreate", e);
        }
        try {
            createWebappParams = intent.getStringExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_NEW_WEBAPP_PARAMS");
            Trace.debug("PebbleWebappBaseActivity", "onNewIntent: createWebappParams = " + createWebappParams);
        } catch (NullPointerException e2) {
            Trace.debug("PebbleWebappBaseActivity", "onCreate", e2);
        }
        if (closeKillWebappParams != null) {
            boolean quiet = onCloseKillWebappIntent(intent, closeKillWebappParams);
        }
        if (createWebappParams != null) {
            quiet = onNewWebappCreationIntent(intent, createWebappParams);
        }
    }

    public static synchronized void createNewWebapp(Context launchingContext, String loadThisUrlString, String requestedByClientId, boolean useBootstrapScripts) {
        synchronized (PebbleWebappBaseActivity.class) {
            createNewWebapp(launchingContext, loadThisUrlString, requestedByClientId, useBootstrapScripts, false);
        }
    }

    public static synchronized void createNewWebapp(Context launchingContext, String loadThisUrlString, String requestedByClientId, boolean useBootstrapScripts, boolean quietLaunch) {
        synchronized (PebbleWebappBaseActivity.class) {
            String pstring;
            Intent i = new Intent(JsAppContextBridge.getInstance().getAppContext(), PebbleWebUiWebappActivity.class);
            try {
                JSONObject jo = new JSONObject();
                jo.put("webappObject", BlankApp.class.getCanonicalName());
                String str = "loadUrlString";
                if (loadThisUrlString == null) {
                    loadThisUrlString = "";
                }
                jo.put(str, loadThisUrlString);
                jo.put("useBootstrapScripts", useBootstrapScripts);
                jo.put("requestedByClientId", requestedByClientId);
                jo.put("quietLaunch", quietLaunch);
                pstring = jo.toString();
            } catch (Exception e) {
                pstring = JsKitConstants.EmptyJsonObjectString();
            }
            i.putExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_NEW_WEBAPP_PARAMS", pstring);
            i.putExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_COUNTER", getCurrentLaunchCounter());
            if (launchingContext == null) {
                launchingContext = JsAppContextBridge.getInstance().getAppContext();
                i.setFlags(268435456);
            }
            launchingContext.startActivity(i);
        }
    }

    public static synchronized void createNewNoUiWebapp(Context launchingContext, String loadThisUrlString, String appInfoJsonUrl, String requestedByClientId, boolean useBootstrapScripts, boolean quietLaunch) {
        synchronized (PebbleWebappBaseActivity.class) {
            String pstring;
            Intent i = new Intent(JsAppContextBridge.getInstance().getAppContext(), PebbleNoUiWebappActivity.class);
            try {
                JSONObject jo = new JSONObject();
                jo.put("webappObject", NoUiBlankApp.class.getCanonicalName());
                String str = "loadUrlString";
                if (loadThisUrlString == null) {
                    loadThisUrlString = "";
                }
                jo.put(str, loadThisUrlString);
                str = "appInfoJsonUrlString";
                if (appInfoJsonUrl == null) {
                    appInfoJsonUrl = "";
                }
                jo.put(str, appInfoJsonUrl);
                jo.put("useBootstrapScripts", useBootstrapScripts);
                jo.put("requestedByClientId", requestedByClientId);
                jo.put("quietLaunch", quietLaunch);
                pstring = jo.toString();
            } catch (Exception e) {
                pstring = JsKitConstants.EmptyJsonObjectString();
            }
            i.putExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_NEW_WEBAPP_PARAMS", pstring);
            i.putExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_COUNTER", getCurrentLaunchCounter());
            if (launchingContext == null) {
                launchingContext = JsAppContextBridge.getInstance().getAppContext();
                i.setFlags(268435456);
            }
            launchingContext.startActivity(i);
        }
    }

    public static synchronized void closeWebapp(Context launchingContext, String targetClientUuidString, WebappTypeE webappType) {
        synchronized (PebbleWebappBaseActivity.class) {
            if (targetClientUuidString != null) {
                Intent i = null;
                switch (AnonymousClass1.$SwitchMap$com$getpebble$android$jskit$webapps$PebbleWebappBaseActivity$WebappTypeE[webappType.ordinal()]) {
                    case ListInfo.INDETERMINATE /*3*/:
                        i = new Intent(JsAppContextBridge.getInstance().getAppContext(), PebbleWebUiWebappActivity.class);
                        break;
                    case SMTPMessage.NOTIFY_DELAY /*4*/:
                        i = new Intent(JsAppContextBridge.getInstance().getAppContext(), PebbleNoUiWebappActivity.class);
                        break;
                }
                if (i != null) {
                    i = modifyIntentForCloseWebapp(i, targetClientUuidString);
                    if (launchingContext == null) {
                        launchingContext = JsAppContextBridge.getInstance().getAppContext();
                        i.setFlags(268435456);
                    }
                    launchingContext.startActivity(i);
                }
            }
        }
    }

    public static synchronized void closeWebapp(Context launchingContext, UUID targetClientUuid, WebappTypeE webappType) {
        synchronized (PebbleWebappBaseActivity.class) {
            String targetClientUuidString = null;
            try {
                targetClientUuidString = targetClientUuid.toString();
            } catch (Exception e) {
                Trace.debug("PebbleWebappBaseActivity", "closeWebapp", e);
            }
            closeWebapp(launchingContext, targetClientUuidString, webappType);
        }
    }

    public static synchronized Intent modifyIntentForCloseWebapp(Intent intent, Object targetClientUuidOrUuidString) {
        Exception e;
        synchronized (PebbleWebappBaseActivity.class) {
            if (intent == null || targetClientUuidOrUuidString == null) {
                intent = null;
            } else {
                JSONObject jo;
                JSONObject jo2;
                String pstring = null;
                if (targetClientUuidOrUuidString instanceof UUID) {
                    pstring = targetClientUuidOrUuidString.toString();
                } else if (targetClientUuidOrUuidString instanceof String) {
                    pstring = (String) targetClientUuidOrUuidString;
                }
                try {
                    jo = new JSONObject(intent.getStringExtra("PEBBLE_WEBAPP_ACTIVITY_CLOSEKILL_WEBAPP_PARAMS"));
                } catch (Exception e2) {
                    jo = null;
                }
                if (jo == null) {
                    try {
                        jo2 = new JSONObject();
                    } catch (Exception e3) {
                        e = e3;
                        jo2 = jo;
                        Trace.debug("PebbleWebappBaseActivity", "modifyIntentForCloseWebapp", e);
                        pstring = JsKitConstants.EmptyJsonObjectString();
                        intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_CLOSEKILL_WEBAPP_PARAMS", pstring);
                        intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_COUNTER", getCurrentLaunchCounter());
                        return intent;
                    }
                }
                jo2 = jo;
                try {
                    Object obj;
                    String str = "targetClientUuidString";
                    if (pstring == null) {
                        obj = "";
                    } else {
                        String str2 = pstring;
                    }
                    jo2.put(str, obj);
                    pstring = jo2.toString();
                } catch (Exception e4) {
                    e = e4;
                    Trace.debug("PebbleWebappBaseActivity", "modifyIntentForCloseWebapp", e);
                    pstring = JsKitConstants.EmptyJsonObjectString();
                    intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_CLOSEKILL_WEBAPP_PARAMS", pstring);
                    intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_COUNTER", getCurrentLaunchCounter());
                    return intent;
                }
                intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_CLOSEKILL_WEBAPP_PARAMS", pstring);
                intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_COUNTER", getCurrentLaunchCounter());
            }
        }
        return intent;
    }

    public static synchronized Intent modifyIntentForCloseMultipleWebapps(Intent intent, Object targetClientUuidsOrUuidStrings) {
        String pstring;
        synchronized (PebbleWebappBaseActivity.class) {
            if (intent == null || targetClientUuidsOrUuidStrings == null) {
                Trace.debug("PebbleWebappBaseActivity", "modifyIntentForCloseMultipleWebapps: params null");
                intent = null;
            } else if (targetClientUuidsOrUuidStrings instanceof UUID) {
                intent = modifyIntentForCloseWebapp(intent, (UUID) targetClientUuidsOrUuidStrings);
            } else if (targetClientUuidsOrUuidStrings instanceof String) {
                intent = modifyIntentForCloseWebapp(intent, (String) targetClientUuidsOrUuidStrings);
            } else {
                JSONObject jo;
                JSONObject jo2;
                JSONArray jsonArray = new JSONArray();
                try {
                    if (targetClientUuidsOrUuidStrings instanceof UUID[]) {
                        for (UUID uuid : (UUID[]) targetClientUuidsOrUuidStrings) {
                            jsonArray.put(uuid.toString());
                        }
                    } else if (targetClientUuidsOrUuidStrings instanceof String[]) {
                        for (String uuidString : (String[]) targetClientUuidsOrUuidStrings) {
                            jsonArray.put(uuidString);
                        }
                    }
                } catch (Exception e) {
                    Trace.debug("PebbleWebappBaseActivity", "modifyIntentForCloseMultipleWebapps", e);
                }
                try {
                    jo = new JSONObject(intent.getStringExtra("PEBBLE_WEBAPP_ACTIVITY_CLOSEKILL_WEBAPP_PARAMS"));
                } catch (Exception e2) {
                    Trace.debug("PebbleWebappBaseActivity", "modifyIntentForCloseMultipleWebapps", e2);
                    jo = null;
                }
                if (jo == null) {
                    try {
                        jo2 = new JSONObject();
                    } catch (Exception e3) {
                        jo2 = jo;
                        pstring = JsKitConstants.EmptyJsonObjectString();
                        intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_CLOSEKILL_WEBAPP_PARAMS", pstring);
                        intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_COUNTER", getCurrentLaunchCounter());
                        return intent;
                    }
                }
                jo2 = jo;
                try {
                    String str = "targetClientUuidString";
                    if (jsonArray == null) {
                        jsonArray = "";
                    }
                    jo2.put(str, jsonArray);
                    pstring = jo2.toString();
                } catch (Exception e4) {
                    pstring = JsKitConstants.EmptyJsonObjectString();
                    intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_CLOSEKILL_WEBAPP_PARAMS", pstring);
                    intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_COUNTER", getCurrentLaunchCounter());
                    return intent;
                }
                intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_CLOSEKILL_WEBAPP_PARAMS", pstring);
                intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_COUNTER", getCurrentLaunchCounter());
            }
        }
        return intent;
    }

    public static synchronized Intent modifyIntentWithRequestedByClientUuid(Intent intent, Object requestedByClientUuidOrUuidString, WebappIntentTypeE intentType) {
        synchronized (PebbleWebappBaseActivity.class) {
            if (intent == null || requestedByClientUuidOrUuidString == null) {
                Trace.debug("PebbleWebappBaseActivity", "modifyIntentWithRequestedByClientUuid: params null");
                intent = null;
            } else {
                JSONObject jo;
                JSONObject jo2;
                String pstring = null;
                if (requestedByClientUuidOrUuidString instanceof UUID) {
                    pstring = requestedByClientUuidOrUuidString.toString();
                } else if (requestedByClientUuidOrUuidString instanceof String) {
                    pstring = (String) requestedByClientUuidOrUuidString;
                }
                try {
                    if (intentType.equals(WebappIntentTypeE.CloseKill)) {
                        jo = new JSONObject(intent.getStringExtra("PEBBLE_WEBAPP_ACTIVITY_CLOSEKILL_WEBAPP_PARAMS"));
                    } else {
                        jo = new JSONObject(intent.getStringExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_NEW_WEBAPP_PARAMS"));
                    }
                    jo2 = jo;
                } catch (Exception e) {
                    Trace.debug("PebbleWebappBaseActivity", "modifyIntentForCloseMultipleWebapps", e);
                    jo2 = null;
                }
                if (jo2 == null) {
                    try {
                        jo = new JSONObject();
                    } catch (Exception e2) {
                        jo = jo2;
                        pstring = JsKitConstants.EmptyJsonObjectString();
                        if (intentType.equals(WebappIntentTypeE.CloseKill)) {
                            intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_NEW_WEBAPP_PARAMS", pstring);
                        } else {
                            intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_CLOSEKILL_WEBAPP_PARAMS", pstring);
                        }
                        intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_COUNTER", getCurrentLaunchCounter());
                        return intent;
                    }
                }
                jo = jo2;
                try {
                    String str = "requestedByClientId";
                    if (pstring == null) {
                        pstring = "";
                    }
                    jo.put(str, pstring);
                    pstring = jo.toString();
                } catch (Exception e3) {
                    pstring = JsKitConstants.EmptyJsonObjectString();
                    if (intentType.equals(WebappIntentTypeE.CloseKill)) {
                        intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_CLOSEKILL_WEBAPP_PARAMS", pstring);
                    } else {
                        intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_NEW_WEBAPP_PARAMS", pstring);
                    }
                    intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_COUNTER", getCurrentLaunchCounter());
                    return intent;
                }
                if (intentType.equals(WebappIntentTypeE.CloseKill)) {
                    intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_CLOSEKILL_WEBAPP_PARAMS", pstring);
                } else {
                    intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_NEW_WEBAPP_PARAMS", pstring);
                }
                intent.putExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_COUNTER", getCurrentLaunchCounter());
            }
        }
        return intent;
    }

    protected static UUID[] extractTargetUuidsFromKillIntentData(String parameterJsonFromIntentData) {
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(parameterJsonFromIntentData);
        } catch (JSONException e) {
            Trace.debug("PebbleWebappBaseActivity", "extractTargetUuidsFromKillIntentData", e);
        }
        if (jsonObject == null) {
            return new UUID[0];
        }
        UUID uuid;
        List<UUID> rv = new ArrayList();
        try {
            JSONArray jsonArray = jsonObject.getJSONArray("targetClientUuidString");
            if (jsonArray != null) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    try {
                        uuid = UUID.fromString(jsonArray.optString(i, null));
                        if (uuid != null) {
                            rv.add(uuid);
                        }
                    } catch (Exception e2) {
                        Trace.debug("PebbleWebappBaseActivity", "extractTargetUuidsFromKillIntentData", e2);
                    }
                }
            }
        } catch (Exception e22) {
            Trace.debug("PebbleWebappBaseActivity", "extractTargetUuidsFromKillIntentData", e22);
        }
        if (rv.size() == 0) {
            try {
                uuid = UUID.fromString(jsonObject.getString("targetClientUuidString"));
                if (uuid != null) {
                    rv.add(uuid);
                }
            } catch (Exception e222) {
                Trace.debug("PebbleWebappBaseActivity", "extractTargetUuidsFromKillIntentData", e222);
            }
        }
        return (UUID[]) rv.toArray(new UUID[rv.size()]);
    }

    protected static UUID extractLinkedClientUuidFromIntentData(Intent intent) {
        if (intent == null) {
            Trace.debug("PebbleWebappBaseActivity", "extractLinkedClientUuidFromIntentData: webappParams null");
            return null;
        }
        String webappParams = null;
        try {
            webappParams = intent.getStringExtra("PEBBLE_WEBAPP_ACTIVITY_CLOSEKILL_WEBAPP_PARAMS");
            if (webappParams == null) {
                webappParams = intent.getStringExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_NEW_WEBAPP_PARAMS");
            }
        } catch (NullPointerException e) {
            Trace.debug("PebbleWebappBaseActivity", "extractLinkedClientUuidFromIntentData", e);
        }
        if (webappParams == null) {
            Trace.debug("PebbleWebappBaseActivity", "extractLinkedClientUuidFromIntentData: webappParams null");
            return null;
        }
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(webappParams);
        } catch (JSONException e2) {
            Trace.debug("PebbleWebappBaseActivity", "extractLinkedClientUuidFromIntentData", e2);
        }
        if (jsonObject == null) {
            Trace.debug("PebbleWebappBaseActivity", "extractLinkedClientUuidFromIntentData: jsonObject null");
            return null;
        }
        try {
            UUID uuid = UUID.fromString(jsonObject.getString("requestedByClientId"));
            if (uuid != null) {
                return uuid;
            }
        } catch (Exception e3) {
            Trace.debug("PebbleWebappBaseActivity", "extractLinkedClientUuidFromIntentData", e3);
        }
        return null;
    }

    protected static WebappIntentTypeE extractIntentTypeFromIntentData(Intent intent) {
        if (intent == null) {
            Trace.debug("PebbleWebappBaseActivity", "extractIntentTypeFromIntentData: intent null");
            return null;
        }
        try {
            if (intent.getStringExtra("PEBBLE_WEBAPP_ACTIVITY_CLOSEKILL_WEBAPP_PARAMS") != null) {
                return WebappIntentTypeE.CloseKill;
            }
            if (intent.getStringExtra("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_NEW_WEBAPP_PARAMS") != null) {
                return WebappIntentTypeE.Open;
            }
            return WebappIntentTypeE.__INVALID__;
        } catch (NullPointerException e) {
            Trace.debug("PebbleWebappBaseActivity", "extractIntentTypeFromIntentData", e);
        }
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public java.lang.Object operatingUnderLock(java.util.List<com.getpebble.android.jskit.webapps.WebappRunHost> r11, java.lang.Object... r12) {
        /*
        r10 = this;
        r9 = 0;
        r8 = 1;
        r7 = 0;
        if (r12 != 0) goto L_0x000e;
    L_0x0005:
        r6 = "PebbleWebappBaseActivity";
        r8 = "operatingUnderLock: optContextDataVararg null";
        com.getpebble.android.common.core.trace.Trace.debug(r6, r8);
        r6 = r7;
    L_0x000d:
        return r6;
    L_0x000e:
        r6 = r12.length;
        if (r6 >= r8) goto L_0x001a;
    L_0x0011:
        r6 = "PebbleWebappBaseActivity";
        r8 = "operatingUnderLock: optContextDataVararg blank";
        com.getpebble.android.common.core.trace.Trace.debug(r6, r8);
        r6 = r7;
        goto L_0x000d;
    L_0x001a:
        r5 = r12[r9];
        r6 = r5 instanceof com.getpebble.android.jskit.webapps.PebbleWebappBaseActivity.QueryOnWebappRunHostListData;
        if (r6 == 0) goto L_0x0036;
    L_0x0020:
        r0 = 0;
        r6 = r12.length;
        if (r6 <= r8) goto L_0x002f;
    L_0x0024:
        r6 = r12.length;
        r6 = r6 + -1;
        r0 = new java.lang.Object[r6];
        r6 = r12.length;
        r6 = r6 + -1;
        java.lang.System.arraycopy(r12, r8, r0, r9, r6);
    L_0x002f:
        r5 = (com.getpebble.android.jskit.webapps.PebbleWebappBaseActivity.QueryOnWebappRunHostListData) r5;
        r6 = r5.query(r11, r7, r0);
        goto L_0x000d;
    L_0x0036:
        r6 = r5 instanceof com.getpebble.android.jskit.webapps.PebbleWebappBaseActivity.OperateOnWebappRunHostListData;
        if (r6 == 0) goto L_0x0043;
    L_0x003a:
        if (r11 == 0) goto L_0x0041;
    L_0x003c:
        r5 = (com.getpebble.android.jskit.webapps.PebbleWebappBaseActivity.OperateOnWebappRunHostListData) r5;
        r5.operate(r11);
    L_0x0041:
        r6 = r7;
        goto L_0x000d;
    L_0x0043:
        r6 = r5 instanceof com.getpebble.android.jskit.webapps.PebbleWebappBaseActivity.NoUiWebappLifecycleOpsE;
        if (r6 == 0) goto L_0x0099;
    L_0x0047:
        r6 = com.getpebble.android.jskit.webapps.PebbleWebappBaseActivity.AnonymousClass1.$SwitchMap$com$getpebble$android$jskit$webapps$PebbleWebappBaseActivity$NoUiWebappLifecycleOpsE;	 Catch:{ Exception -> 0x0062 }
        r5 = (com.getpebble.android.jskit.webapps.PebbleWebappBaseActivity.NoUiWebappLifecycleOpsE) r5;	 Catch:{ Exception -> 0x0062 }
        r8 = r5.ordinal();	 Catch:{ Exception -> 0x0062 }
        r6 = r6[r8];	 Catch:{ Exception -> 0x0062 }
        switch(r6) {
            case 1: goto L_0x0054;
            case 2: goto L_0x0056;
            case 3: goto L_0x005e;
            case 4: goto L_0x006b;
            case 5: goto L_0x006f;
            case 6: goto L_0x0073;
            default: goto L_0x0054;
        };	 Catch:{ Exception -> 0x0062 }
    L_0x0054:
        r6 = r7;
        goto L_0x000d;
    L_0x0056:
        r6 = 1;
        r6 = r12[r6];	 Catch:{ Exception -> 0x0062 }
        r6 = (android.os.Bundle) r6;	 Catch:{ Exception -> 0x0062 }
        r10.forwardToWebAppsOnCreate(r11, r6);	 Catch:{ Exception -> 0x0062 }
    L_0x005e:
        r10.forwardToWebAppsOnResume(r11);	 Catch:{ Exception -> 0x0062 }
        goto L_0x0054;
    L_0x0062:
        r1 = move-exception;
        r6 = "PebbleWebappBaseActivity";
        r8 = "operatingUnderLock";
        com.getpebble.android.common.core.trace.Trace.debug(r6, r8, r1);
        goto L_0x0054;
    L_0x006b:
        r10.forwardToWebAppsOnPause(r11);	 Catch:{ Exception -> 0x0062 }
        goto L_0x0054;
    L_0x006f:
        r10.forwardToWebAppsOnDestroy(r11);	 Catch:{ Exception -> 0x0062 }
        goto L_0x0054;
    L_0x0073:
        r6 = 1;
        r4 = r12[r6];	 Catch:{ Exception -> 0x0090 }
        r4 = (java.lang.String) r4;	 Catch:{ Exception -> 0x0090 }
        if (r4 == 0) goto L_0x0054;
    L_0x007a:
        r3 = r10.findInList(r4, r11);	 Catch:{ Exception -> 0x0090 }
        if (r3 < 0) goto L_0x0054;
    L_0x0080:
        r2 = r11.get(r3);	 Catch:{ Exception -> 0x0090 }
        r2 = (com.getpebble.android.jskit.webapps.NoUiWebappBase) r2;	 Catch:{ Exception -> 0x0090 }
        r2.flagForSubsequentTeardownOnDestroy();	 Catch:{ Exception -> 0x0090 }
        r2.onDestroy();	 Catch:{ Exception -> 0x0090 }
        r11.remove(r3);	 Catch:{ Exception -> 0x0090 }
        goto L_0x0054;
    L_0x0090:
        r1 = move-exception;
        r6 = "PebbleWebappBaseActivity";
        r8 = "operatingUnderLock";
        com.getpebble.android.common.core.trace.Trace.debug(r6, r8, r1);	 Catch:{ Exception -> 0x0062 }
        goto L_0x0054;
    L_0x0099:
        r6 = r7;
        goto L_0x000d;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.getpebble.android.jskit.webapps.PebbleWebappBaseActivity.operatingUnderLock(java.util.List, java.lang.Object[]):java.lang.Object");
    }

    protected int findInList(String uuidString, List<WebappRunHost> list) {
        for (int i = 0; i < list.size(); i++) {
            WebappRunHost o = (WebappRunHost) list.get(i);
            if (o != null && o.clientUuidAsString().equalsIgnoreCase(uuidString)) {
                return i;
            }
        }
        return -1;
    }

    protected void doKillSequenceOnSpecifiedClientUuids(Intent causedByIntent, UUID[] uuids) {
        if (uuids == null) {
            Trace.debug("PebbleWebappBaseActivity", "doKillSequenceOnSpecifiedClientUuids: uids is null");
            return;
        }
        for (UUID uuid : uuids) {
            doKillSequenceOnSpecifiedClientUuid(causedByIntent, uuid);
        }
    }

    protected void doKillSequenceOnSpecifiedClientUuid(Intent causedByIntent, String uuidString) {
        try {
            doKillSequenceOnSpecifiedClientUuid(causedByIntent, UUID.fromString(uuidString));
        } catch (Exception e) {
            Trace.debug("PebbleWebappBaseActivity", "doKillSequenceOnSpecifiedClientUuid", e);
        }
    }

    protected void doKillSequenceOnSpecifiedClientUuid(Intent causedByIntent, UUID uuid) {
        if (uuid == null) {
            Trace.debug("PebbleWebappBaseActivity", "doKillSequenceOnSpecifiedClientUuid: uuid is null");
            return;
        }
        UUID hostClientUuid = JsKit.jsKitAccess().reverseCurrentActiveAppUuid(uuid);
        if (hostClientUuid == null) {
            hostClientUuid = uuid;
        }
        if (hostClientUuid != null) {
            NoUiAppsList.getInstance().noUiAppsListLockedAccess(this, NoUiWebappLifecycleOpsE.ExplicitKill, hostClientUuid.toString());
        }
        JsKit.jsKitAccess().kill(hostClientUuid, uuid, this instanceof PebbleNoUiWebappActivity);
    }

    protected void forwardToWebAppsOnDestroy(List<WebappRunHost> list) {
        if (list != null) {
            for (WebappRunHost noUiWebapp : list) {
                if (noUiWebapp != null) {
                    noUiWebapp.onDestroy();
                }
            }
        }
    }

    protected void forwardToWebAppsOnCreate(List<WebappRunHost> list, Bundle savedInstanceState) {
        if (list != null) {
            for (WebappRunHost noUiWebapp : list) {
                if (noUiWebapp != null) {
                    noUiWebapp.onCreate(savedInstanceState);
                }
            }
        }
    }

    protected void forwardToWebAppsOnPause(List<WebappRunHost> list) {
        if (list != null) {
            for (WebappRunHost noUiWebapp : list) {
                if (noUiWebapp != null) {
                    noUiWebapp.onPause();
                }
            }
        }
    }

    protected void forwardToWebAppsOnResume(List<WebappRunHost> list) {
        if (list != null) {
            for (WebappRunHost noUiWebapp : list) {
                if (noUiWebapp != null) {
                    noUiWebapp.onResume();
                }
            }
        }
    }

    protected void onPause() {
        super.onPause();
        this.isPaused = true;
        LogSimpleDebug(this, "+++++++++++++++++ onPause", new Object[0]);
    }

    protected void onResume() {
        super.onResume();
        this.isPaused = false;
        LogSimpleDebug(this, "+++++++++++++++++ onResume", new Object[0]);
    }

    protected void onDestroy() {
        super.onDestroy();
        this.isPaused = false;
        LogSimpleDebug(this, "+++++++++++++++++ onDestroy", new Object[0]);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    }

    protected void onSaveInstanceState(Bundle outState) {
        this.isPaused = true;
        LogSimpleDebug("+++++++++++++++++ onSaveInstanceState");
        outState.putLong("PEBBLE_WEBAPP_ACTIVITY_LAUNCH_COUNTER", getCurrentLaunchCounter());
    }
}
